var time = require("../../../util/time");
var consts = require("../../../util/consts");
const { getUpgradeDate, getUserData } = require("../../box/lib/api");
/**
 * 检查是否有签到缓存
 * @param {*} uid 
 */
exports.checkUserSign = function(uid){
    return new Promise((resolve,reject)=>{
        let redis = this.app.get("redis");
        let redis_key = consts.RedisPrefix.SIGN+time.getWeekStartDate();
        redis.hexists(redis_key,uid,(err,data)=>{
            if(err) return reject(err);
            resolve(data!=0);
        })
    })    
}
/**
 * 获取用户缓存数据
 * @param {*} uid 
 */
exports.getUserSignData = function(uid){
    return new Promise((resolve,reject)=>{
        let redis = this.app.get("redis");
        let redis_key = consts.RedisPrefix.SIGN+time.getWeekStartDate();
        redis.hget(redis_key,uid,(err,data)=>{
            if(err) return reject(err);
            resolve(JSON.parse(data));
        })
    })
}
exports.saveUserSign = function(signData){
    return new Promise((resolve,reject)=>{
        let redis = this.app.get("redis");
        let redis_key = consts.RedisPrefix.SIGN+time.getWeekStartDate();
        redis.hset(redis_key,signData.uid,JSON.stringify(signData.data),(err,data)=>{
            if(err) return reject(err);
            resolve(JSON.parse(data));
        })
    })
}
//获得每秒产金
exports.getUserPrePorduct =  function(session){
    let self = this;
    return new Promise((resolve,reject)=>{
        self.app.rpc.upgrade.upgradeRemote.getPreProduct(session, session.uid,function(err,product){
            if(err){
               return reject(err);
            }
            resolve(product);
        })
    })
}

//每日石油上限
exports.getTodayOilLimit = async function(session,uid){
  //获取升级相关数据
  let {depth_level, quality_level, spreed_level, incomeRate} = await getUpgradeDate.call(this, session);
  //获取视频次数，徒弟数，徒孙数
  let {video_num, apprentice_num, disciple_num} = await getUserData.call(this, session);
  let commanderLevel = getCommanderLevel(apprentice_num,disciple_num)
  let rate = 20;
  if(video_num < consts.Constant.DAILY_VIDEO_TIME){
      rate = 10
  }

  let maxOilFormula = depth_level/100 + quality_level/100 + spreed_level/100 + video_num/30 + apprentice_num/100 + commanderLevel / 6;
  return maxOilFormula*rate;
}

exports.GetCommanderLevel = async function(session) {
    let {video_num, apprentice_num, disciple_num} = await getUserData.call(this, session);
    let commanderLevel = getCommanderLevel(apprentice_num,disciple_num)
    return commanderLevel; 
}


//获得团长等级
let getCommanderLevel = function(apprentice_num,disciple_num){
    if(apprentice_num>=1000&&disciple_num>=3000){
        return 6;
    }else if(apprentice_num>=500&&disciple_num>=1500){
        return 5;
    }else if(apprentice_num>=200&&disciple_num>=600){
        return 4;
    }else if(apprentice_num>=50&&disciple_num>=150){
        return 3;
    }else if(apprentice_num>=3){
        return 2;
    }else{
        return 1;
    }
}